var data = require("./data.js");
var clickjs = require("./click.js");

var define = data.define;

var blocks = [];

function createBlock(map, mapW, mapH, width, mapMouse) {
  if (!clickjs.canInput(map, mapW, mapH, width, mapMouse))
    return;

  var b = {};
  b.x = parseInt(clickjs.getX());
  b.y = parseInt(clickjs.getY());
  b.times = 0;

  blocks.push(b);
  showBlock(b, map, mapW, mapH, width, mapMouse);
}

function showBlock(b, map, mapW, mapH, width, mapMouse)
{
  map[b.x][b.y] = define.Block;
}

function clearBlock(b, map, mapW, mapH, width, mapMouse)
{
  map[b.x][b.y] = 0;
}

function updateBlocks(map, mapW, mapH, width, mapMouse) {
  var temp = [];
  for (var i = 0; i < blocks.length; i++) {
    blocks[i].times++;
    if (blocks[i].times >= (define.WapBlockTime / define.ShowDelay)) {
      clearBlock(blocks[i], map, mapW, mapH, width, mapMouse);
      continue;
    }
    else {
      showBlock(blocks[i], map, mapW, mapH, width, mapMouse);
      temp.push(blocks[i]);
    }
  }
  blocks = temp;
}

function getNum() {
  return blocks.length;
}

function clear() {
  blocks = [];
}

function getInfo() {
  return blocks;
}

module.exports = {
  createBlock: createBlock,
  updateBlocks: updateBlocks,
  getNum: getNum,
  clear: clear,
  getInfo: getInfo
}